package com.cong.security.core.social.app;

import java.util.Collection;

import lombok.Data;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Data
public class OpenIdAuthenticationToken extends AbstractAuthenticationToken {
    private static final long serialVersionUID = 1L;
    // 用户openId
    private Object principal;
    //用户登录方式(本系统中为qq/weixin)
    private String providerId;
    //设备标识(未绑定情况)
    private String clientId;

    /**
     * 构造函数
     *
     * @param openId     用户openId
     * @param clientId   客户端设备编号
     * @param providerId 用户登录方式
     */
    public OpenIdAuthenticationToken(String openId, String clientId, String providerId) {
        super(null);
        // 用户openId
        this.principal = openId;
        this.clientId = clientId;
        log.info("当前用户[{}]在设备[{}]上采用[{}]模式登录系统", openId, clientId, providerId);
        // 是哪一个服务提供商的
        this.providerId = providerId;
        setAuthenticated(false);
    }

    public OpenIdAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        super.setAuthenticated(true);
    }

    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException(
                    "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        }
        super.setAuthenticated(false);
    }

    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }
}
